package com.cincc.cinccimdemo.ui.contact;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.adapter.ContactAdapter;
import com.cincc.cinccimdemo.common.ContactUtil;
import com.cincc.cinccimdemo.model.FriendInfoUI;
import com.cincc.cinccimdemo.ui.main.UserInfoActivity;
import com.cincc.cinccimdemo.viewmodel.ContactViewModel;
import com.cincc.cinccimdemo.widget.SideBarView;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.listener.ImFetchFriendInfoCompleteListener;

import java.util.ArrayList;
import java.util.List;

public class ContactFragment extends Fragment implements ImFetchFriendInfoCompleteListener {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contact, container, false);
        initView(view);
        return view;
    }

    List<FriendInfoUI> list = new ArrayList<>();
    ContactAdapter contactAdapter;
    RecyclerView recyclerView;
    SideBarView sideBarView;
    TextView mTextCenterContact;

    ContactViewModel contactViewModel;

    private void initView(View view) {
        recyclerView = view.findViewById(R.id.mList_contact);
        contactAdapter = new ContactAdapter(requireActivity(), list);
        recyclerView.setAdapter(contactAdapter);
        sideBarView = view.findViewById(R.id.mSide_bar_contact);
        mTextCenterContact = view.findViewById(R.id.mText_center_contact);
        contactAdapter.setOnHeaderClickItem(this::openPage);
        contactAdapter.setOnDataClickItem(this::openDetail);

        contactViewModel = new ViewModelProvider(this).get(ContactViewModel.class);
        contactViewModel.contactListLiveData().observe(getViewLifecycleOwner(), friendInfos -> {
            contactAdapter.setContactList(friendInfos);
            setSideView();
        });
    }

    public void refreshData() {
        IMSipPhoneCtrl.getInstance().refreshFriendList(imResponse -> contactViewModel.setContact());
    }

    private void openPage(int type) {
        if (type == 1) {
            Intent i = new Intent(requireActivity(), GroupListActivity.class);
            startActivity(i);
        }
    }

    private void openDetail(int index) {
        FriendInfoUI item = list.get(index);
        UserInfoActivity.startUserDetail(requireContext(), item.userId);
    }

    public void setSideView() {
        // 设置需要显示的索引栏内容
        String[] letters = ContactUtil.getLetters(1, contactAdapter.getList());
        sideBarView.setLetter(letters);
        // 设置需要显示的提示框
        sideBarView.setTextView(mTextCenterContact);
        sideBarView.setOnTouchingLetterChangedListener(s -> {
            int position = ContactUtil.getLetterPosition(list, s, 1);
            if (position != -1) {
                recyclerView.scrollToPosition(position);
            }
        });
    }

    @Override
    public void onFetchFriendInfoComplete() {
        refreshData();
    }
}
